package com.kly.user.serviceImpl.traffic.likeback;

import com.fqgj.common.api.Response;
import com.kly.user.db.entity.IssueConfigEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.enums.AppEnum;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
public class MebooLikeBack extends LikeBackBase {


    @PostConstruct
    public void init() {
        LikebackTrafficHandler.likeBackBaseMap.put(AppEnum.MEBOO.getUserAppFlag(), this);
        LikebackTrafficHandler.likeBackBaseMap.put(AppEnum.MEBOO_IOS.getUserAppFlag(), this);
    }

    @Override
    public void dealCondition(IssueConfigEntity issueConfigEntity, UserEntity userEntity) {
        List<Integer> wlmTypes = new ArrayList<>();
        wlmTypes.add(3);
        Integer likeBackCount = userWlmDao.getCountByWmlCode(wlmTypes, userEntity.getUserCode());
        logger.info("==likebackV5 开始执行likeback 给用户:{} 今日的likeback数量:{} ==", userEntity.getUserCode(), likeBackCount);
        wlmTypes.add(1);
        Integer likeCount = userWlmDao.getCountByLikesCode(wlmTypes, userEntity.getUserCode());
        logger.info("==likebackV5 开始执行likeback 给用户:{} 今日的喜欢的数量:{} ==", userEntity.getUserCode(), likeCount);

        if (likeCount == 0) {
            logger.info("==likebackV5 喜欢的数量为0 ，没有likeback可以下发 给用户:{} 今日的喜欢的数量:{} ==", userEntity.getUserCode(), likeCount);
            return;
        }

        float countResult = ((likeCount * issueConfigEntity.getLkOne()) / 100.0f) - likeBackCount;
        logger.info("==likebackV5 开始执行likeback 给用户:{} 今日的likeback数量:{}，今日的喜欢的数量:{} ，策略下剩余可以likeback的数量:{}==", userEntity.getUserCode(), likeBackCount, likeCount, countResult);
        if (countResult <= 0) {
            logger.info("==likebackV5 开始执行likeback 策略下likeback数量已到顶  给用户:{} 今日的likeback数量:{} ==", userEntity.getUserCode(), likeBackCount);
            return;
        }

        Response<Boolean> response = memberService.ifMember(userEntity.getUserCode());
        if (!response.isSuccess()) {
            logger.error("==likebackV5 开始执行likeback  查询会员状态信息失败 给用户:{} 今日的喜欢的数量:{} ==", userEntity.getUserCode(), likeCount);
            return;
        }

        Boolean ifmMember = response.getData();
        if (ifmMember) {
            if (likeBackCount < issueConfigEntity.getMemberNum()) {
                if (likeCount == 1 || likeBackCount == 0) {
                    dealLikeOne(issueConfigEntity, userEntity, ifmMember);
                } else {
                    dealLikeMoreOne(issueConfigEntity, userEntity, likeBackCount, ifmMember);
                }
            }
        } else {
            if (likeBackCount < issueConfigEntity.getNonmemberNum()) {
                if (likeCount == 1 || likeBackCount == 0) {
                    dealLikeOne(issueConfigEntity, userEntity, ifmMember);
                } else {
                    dealLikeMoreOne(issueConfigEntity, userEntity, likeBackCount, ifmMember);
                }
            }
        }

    }



}
